package com.lelouch.service;

import com.google.common.base.Preconditions;
import com.lelouch.bean.SysDept;
import com.lelouch.bean.SysUser;
import com.lelouch.beans.PageQuery;
import com.lelouch.beans.PageResult;
import com.lelouch.common.RequestHolder;
import com.lelouch.dao.SysDeptMapper;
import com.lelouch.dao.SysUserMapper;
import com.lelouch.exception.ParamException;
import com.lelouch.param.DeptParam;
import com.lelouch.param.UserParam;
import com.lelouch.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户服务
 *
 * @author lcy
 * @create 2018-06-12 15:08
 */
@Service
public class SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysLogService sysLogService;


    public void save(UserParam userParam) {
        //检查参数是否合法
        BeanValidator.check(userParam);
        if (checkTelephone(userParam.getTelephone(), userParam.getId())) {
            throw new ParamException("电话号码已经被占用！");
        }
        if (checkEmail(userParam.getMail(), userParam.getId())) {
            throw new ParamException("电子邮件已经被占用！");
        }

        SysUser sysUser = SysUser.builder().username(userParam.getUsername()).telephone(userParam.getTelephone())
                .mail(userParam.getMail()).deptId(userParam.getDeptId()).remark(userParam.getRemark()).status(userParam.getStatus())
                .build();
        String encryptedPassword = MD5Util.encrypt("12345678");
        sysUser.setPassword(encryptedPassword);
        sysUser.setOperator(RequestHolder.getUser().getUsername());
        sysUser.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        sysUser.setOperateTime(new Date());
        sysUserMapper.insertSelective(sysUser);
        sysLogService.saveUserLog(null,sysUser);

    }

    public void update(UserParam userParam) {
        //检查参数是否合法
        BeanValidator.check(userParam);
        if (checkTelephone(userParam.getTelephone(), userParam.getId())) {
            throw new ParamException("电话号码已经被占用！");
        }
        if (checkEmail(userParam.getMail(), userParam.getId())) {
            throw new ParamException("电子邮件已经被占用！");
        }
        SysUser oldUser = sysUserMapper.selectByPrimaryKey(userParam.getId());
        Preconditions.checkNotNull(oldUser, "待更新的用户不存在");
        SysUser newUser = SysUser.builder().id(userParam.getId()).username(userParam.getUsername()).telephone(userParam.getTelephone())
                .mail(userParam.getMail()).deptId(userParam.getDeptId()).remark(userParam.getRemark()).status(userParam.getStatus())
                .build();
        newUser.setOperator(RequestHolder.getUser().getUsername());//todo
        newUser.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getRequest()));
        newUser.setOperateTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(newUser);
        sysLogService.saveUserLog(oldUser,newUser);
    }


    private boolean checkTelephone(String telephone, Integer id) {
        return sysUserMapper.countTelephone(telephone, id) > 0;
    }

    private boolean checkEmail(String mail, Integer id) {
        return sysUserMapper.countMail(mail, id) > 0;
    }

    public SysUser findByKeyword(String username) {
        return sysUserMapper.findByKeyword(username);
    }

    public PageResult<SysUser> pageByDeptId(Integer deptId, PageQuery pageQuery){
        //List<SysUser> list = sysUserMapper.pageByDeptId(deptId,pageQuery);
        //Integer count = sysUserMapper.getUserCountByDeptId(deptId);
        //PageResult<SysUser> pageResult = PageResult.builder().total(count).data(list).build();
        //
        //return pageResult;
        BeanValidator.check(pageQuery);
        int count = sysUserMapper.getUserCountByDeptId(deptId);
        if (count > 0) {
            List<SysUser> list = sysUserMapper.pageByDeptId(deptId,pageQuery);
            return PageResult.<SysUser>builder().total(count).data(list).build();
        }
        return PageResult.<SysUser>builder().build();
    }

    public List<SysUser> getAll() {
        return sysUserMapper.getAll();
    }
}
